package org.zsk.cs.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zsk.cs.domain.po.Menu;
import org.zsk.cs.dao.MenuDao;
import org.zsk.cs.domain.vo.MenuVo;
import org.zsk.cs.service.MenuService;
import org.zsk.fastdev.assign.Assign;
import org.zsk.fastdev.bean.BeanUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 菜单 serviceImpl
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuDao,Menu> implements MenuService {

    private final MenuDao menuDao;
    private final Assign assign;

    public MenuServiceImpl(MenuDao menuDao, Assign assign) {
        this.menuDao = menuDao;
        this.assign = assign;
    }


    @CacheEvict(value = "redisCache", key = "'redis_menu_all'", beforeInvocation = false)
    @Override
    public boolean save(Menu entity) {
        return super.save(entity);
    }

    @Cacheable(value = "redisCache", key = "'redis_menu_all'")
    @Override
    public List<Menu> list() {
        log.debug("获取所有权限");
        return super.list();
    }

    @CacheEvict(value = "redisCache", key = "'redis_menu_all'", beforeInvocation = false)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    @Override
    public Page<Menu> search(MenuVo menuVo) throws Exception {
        Menu menu=new Menu();
        Page<Menu> page=new Page<>();
        assign.assign(menuVo,menu,"pattern,description");
        assign.assign(menuVo,page,"$current,$size");
        return menuDao.selectPage(page, BeanUtils.bean2QueryWrapper(Menu.class,menu));
    }
}
